Android期末考考前盘点(六):1000%用到的Intent意图

您所在的位置:网站首页 activity intent传值 Android期末考考前盘点(六):1000%用到的Intent意图

Android期末考考前盘点(六):1000%用到的Intent意图

2023-07-05 00:04| 来源: 网络整理| 查看: 265

Intent意图,是个非常基础的东西,因为贯穿应用始终,例如跳转Activity、打开Service、向Activity跳转并传递数据、回传数据等等......

首先要知道这个Intent,是一个系统类,所以你使用时,第一件事必然是new对象

Intent intent=new Intent(Lindd.this,Zhuzhendonghua.class); //跳转Activity Lindd到Zhuzhendonghua

上面的代码就是从Lindd这个Activity跳转到Zhuzhendonghua这个Actiyity

所以当想要跳转Activity的时候,第一个参数是上下文(粗暴的理解就是现在在哪),第二个参数就是去哪里

再执行以下代码即可跳转

startActivity(intent);//跳转

再比如,在Service服务中对服务进行启动:

Intent intent=new Intent(MainActivity.this, MyService.class); startService(intent);

接下来就是其中较为复杂的跳转Activity并携带数据

这里,在课堂中教过使用Bundel

Intent intent=new Intent(Lindd.this,ZIdingyikongjian.class); Bundle bundle=new Bundle(); bundle.putString("account","lindd"); bundle.putString("password","123123"); intent.putExtras(bundle); startActivity(intent);

但是对于基础差的学生我建议老老实实的多put几个,如下:

Intent intent=new Intent(Lindd.this,ZIdingyikongjian.class); intent.putExtra("account","lindd"); intent.putExtra("password","123123"); startActivity(intent);

对于法一,在跳转的Activity中如何获取?如下代码

Bundle bundle= getIntent().getExtras(); String account=bundle.getString("account"); String password=bundle.getString("password");

对于法二,在跳转的Activity中如何获取?如下代码

getIntent().getStringExtra("account"); getIntent().getStringExtra("password");



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3